package com.jf.design.singleton;

import static java.lang.System.out;

/**
 * 双端检查锁 单例模式
 *
 * @author Junfeng
 */
public class DoubleCheckLockSingleton {

    private static volatile DoubleCheckLockSingleton INSTANCE;

    private DoubleCheckLockSingleton() {
        out.println(" -[DEBUG]-   " + Thread.currentThread().getName() + " mew instance");
    }

    public static DoubleCheckLockSingleton getInstance() {
        if (null == INSTANCE) {
            synchronized (DoubleCheckLockSingleton.class) {
                if (null == INSTANCE) {
                    INSTANCE = new DoubleCheckLockSingleton();
                }
            }
        }
        return INSTANCE;
    }

    public static void main(String[] arg) {
        for (int i = 0; i < 20; i++) {
            new Thread(() -> {
                DoubleCheckLockSingleton.getInstance();
            }, "T-" + i).start();
        }
    }
}
